﻿using Anke.SHManage.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.MSSQLDAL
{
    public partial class M_PatientRecordTraceDAL : BaseDAL<M_PatientRecordTrace>
    {
        public static readonly SqlConnectionStringBuilder ManageBuilder = new SqlConnectionStringBuilder(SqlHelper.MainConnectionString);//取管理库的链接字符串
       //病历痕迹列表
        public object GetPRTraceList(int pageIndex, int pageSize, string TaskCode, int PatientOrder)
        {
            List<M_PatientRecordTraceExt> list = new List<M_PatientRecordTraceExt>();
            StringBuilder sb = new StringBuilder();
            sb.Append(@" select  distinct identity(int,1,1) as 行号,修改时间=ModifyTime ,修改人=ModifyPerson,任务编码=TaskCode 
                                 ,序号=PatientOrder,修改项=ModifyItem ,原值=OriginalValue ,新值=NewValue ,归属类型=OwnershipType
                        into #tempa
	                    from M_PatientRecordTrace
	                where TaskCode='" + TaskCode + "'  and PatientOrder='" + PatientOrder + "'");
            sb.Append(" order by 修改时间 desc ");
            sb.Append(@" select * into #temp from #tempa where 1=1 ");
            sb.Append(@" select top " + pageIndex + " A.*  from #temp A where 行号>" + (pageSize - 1) * pageIndex + " order by 行号 ");
            sb.Append(@"  SELECT count(*) FROM #temp t   drop table #tempa,#temp ");
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    M_PatientRecordTraceExt info = new M_PatientRecordTraceExt();
                    info.ModifyTime = dr["修改时间"].ToString();
                    info.ModifyPerson = dr["修改人"].ToString();
                    info.ModifyItem = dr["修改项"].ToString();
                    info.NewValue = dr["新值"].ToString();
                    info.OwnershipType = dr["归属类型"].ToString();
                    info.OriginalValue = dr["原值"].ToString();
                    info.TaskCode = dr["任务编码"].ToString();
                    info.PatientOrder = dr["序号"].ToString();
                    list.Add(info);
                }
            }
            int total = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            var result = new { total = total, rows = list };
            return result;
        }
    }

}
